<template>
  <div v-loading="loading" class="addExpense">
    <el-form ref="form" :model="form" :rules="roleCodeRule" label-width="120px">
      <el-row>
        <mainTilte title="基本信息" />
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="商品主图">
            <el-image v-if="form.image_url" :src="form.image_url" class="img" fit="cover" :preview-src-list="[ form.image_url ]" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-form-item>
        </el-col>
        <el-col :span="18">

          <el-col :span="8">
            <el-form-item label="商品分类">
              <product-category v-model="form.category_id" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="系统sku">
              <el-input v-model.trim="form.code" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="中文名称">
              <el-input v-model.trim="form.name_ch" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="英文名称" prop="name_en">
              <el-input v-model="form.name_en" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="运营类型">
              <el-input v-model.trim="form.operate_type" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item v-if="form.type === 0" label="UPC条码">
              <el-input v-model.trim="form.bar_code_upc" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品条码">
              <el-input v-model.trim="form.bar_code" disabled />
            </el-form-item>
          </el-col>
          <el-col v-if="form.type == 1" :span="8">
            <el-form-item label="产品条码2">
              <el-input v-model.trim="form.bar_code2" disabled />
            </el-form-item>
          </el-col>
          <!-- <el-col v-if="form.type === 0 && disabled" :span="8">
            <el-form-item label="所属供应商">
              <el-input v-model.trim="form.supplier_name" disabled />
            </el-form-item>
          </el-col> -->
          <el-col v-if="form.type === 0" :span="8">
            <el-form-item label="采购员">
              <el-input v-model.trim="form.purchase_name" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属客户">
              <el-input v-model.trim="form.company_name" disabled />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
          <el-form-item label="产品条码1" prop="domestic_trans.traile_fee">
            <el-input v-model.trim="form.domestic_trans.traile_fee"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="产品条码2" prop="domestic_trans.traile_fee">
            <el-input v-model.trim="form.domestic_trans.traile_fee"></el-input>
          </el-form-item>
        </el-col> -->
          <!-- <el-col v-if="form.type == 0 && form.purchase_price_rmb && disabled" :span="8">
            <el-form-item label="采购价(RMB)">
              <el-input v-model.trim="form.purchase_price_rmb" disabled />
            </el-form-item>
          </el-col>
          <el-col v-if="form.type == 0 && form.purchase_price_usd && disabled" :span="8">
            <el-form-item label="采购价(USD)">
              <el-input v-model.trim="form.purchase_price_usd" disabled />
            </el-form-item>
          </el-col> -->
          <!-- <el-col v-if="form.type == 1" :span="8">
            <el-form-item label="申报单价(USD)">
              <el-input v-model.trim="form.purchase_price_usd" disabled />
            </el-form-item>
          </el-col> -->
          <el-col v-if="form.type == 1" :span="8">
            <el-form-item label="保险价值(USD)">
              <el-input v-model.trim="form.insured_price_usd" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否保价">
              <el-input v-model.trim="form.insured_price" disabled />
            </el-form-item>
          </el-col>
          <el-col v-if="form.insured_price == '保价'" :span="8">
            <el-form-item label="保值">
              <el-input v-model.trim="form.hedge_price" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="包装方式">
              <el-input v-model.trim="form.packing_method" disabled />
            </el-form-item>
          </el-col>
          <el-col v-if="form.packing_method == '普通商品'" :span="8">
            <el-form-item label="可合并发货数量">
              <el-input v-model.trim="form.merge_num" disabled />
            </el-form-item>
          </el-col>
        </el-col>
      </el-row>
      <div v-if="form.otherData">
        <el-row>
          <mainTilte title="产品信息" />
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="尺寸">
              <el-input v-model="form.otherData.size" disabled type="textarea" autosize />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="重量">
              <el-input v-model="form.otherData.weight" disabled type="textarea" autosize />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="颜色">
              <el-input v-model="form.otherData.color" disabled type="textarea" autosize />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="材质">
              <el-input v-model="form.otherData.material" type="textarea" autosize disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="配件">
              <el-input v-model.trim="form.otherData.parts" type="textarea" autosize disabled />
            </el-form-item>
          </el-col>
          <el-col v-if="form.type == 1" :span="6">
            <el-form-item label="用途">
              <el-input v-model.trim="form.purpose" type="textarea" autosize disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="其他备注">
              <el-input v-model="form.otherData.other_remark" type="textarea" autosize disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </div>

      <el-row>
        <mainTilte title="关税信息" />
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="国内(HS)">
            <el-input v-model.trim="form.ZH_HS" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="国外(HS)">
            <el-input v-model.trim="form.EN_HS" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="国内退税率">
            <el-input v-model.trim="form.tax_rebate_rate" disabled>
              <span slot="append">%</span>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="国外关税税率">
            <el-input v-model.trim="form.tax_tariff_rate" disabled>
              <span slot="append">%</span>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div v-if="
          form.packing_method == '多箱包装' &&
            form.groupData &&
            form.groupData.length > 0
        " class="mb24">
        <el-row>
          <mainTilte title="商品分组" />
        </el-row>
        <el-row v-if="form.groupData && form.groupData.length > 0">
          <el-table :data="form.groupData" stripe fit border>
            <el-table-column prop="number" label="名称" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.name }}</p>
              </template>
            </el-table-column>
            <!-- <el-table-column
              prop="number"
              label="数量"
              width="180"
              align="center"
            >
              <template slot-scope="scope">
                <p>{{ scope.row.number }}</p>
              </template>
            </el-table-column> -->
            <el-table-column prop="number" label="尺寸公制(长)" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.length }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="number" label="尺寸公制(宽)" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.width }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="number" label="尺寸公制(高)" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.height }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="volume" label="体积(公制)" width="180" align="center">
              <template slot-scope="scope">
                {{ scope.row.volume }}
              </template>
            </el-table-column>
            <el-table-column prop="length_AS" label="尺寸美制(长)" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.length_AS }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="width_AS" label="尺寸美制(宽)" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.width_AS }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="height_AS" label="尺寸美制(高)" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.height_AS }}</p>
              </template>
            </el-table-column>
            <!-- <el-table-column
              prop="volume_AS"
              label="体积(美制)"
              width="180"
              align="center"
            >
              <template slot-scope="scope">
                {{ scope.row.volume_AS }}
              </template>
            </el-table-column> -->
            <el-table-column prop="volume_weight_AS" label="体积重(美制)" width="180" align="center">
              <template slot-scope="scope">
                {{ scope.row.volume_weight_AS }}
              </template>
            </el-table-column>
            <el-table-column prop="weight_gross_AS" label="毛重(公制)" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.weight_gross }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="weight_gross_AS" label="毛重(美制)" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.weight_gross_AS }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="weight_AS" label="净重(公制)" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.weight }}</p>
              </template>
            </el-table-column>

            <el-table-column prop="weight_AS" label="净重(美制)" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.weight_AS }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="oversize" label="Oversize" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.oversize }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="hedge_price" label="保值" width="180" align="center">
              <template slot-scope="scope">
                <p>{{ scope.row.hedge_price }}</p>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </div>
      <div v-if="form.packData && form.packing_method === '普通商品'">
        <el-row>
          <mainTilte title="包装信息" />
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="尺寸(公制)">
              <div class="sizeStyle">
                <div>
                  <el-input v-model="form.packData.length" disabled>
                    <template slot="append">CM</template>
                  </el-input>
                </div>
                <div style="margin-left: 8px">
                  <el-input v-model="form.packData.width" disabled>
                    <template slot="append">CM</template>
                  </el-input>
                </div>
                <div style="margin-left: 8px">
                  <el-input v-model="form.packData.height" disabled>
                    <template slot="append">CM</template>
                  </el-input>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="体积(公制)">
              <el-input v-model.trim="form.packData.volume" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="尺寸(美制)">
              <div class="sizeStyle">
                <div>
                  <el-input v-model="form.packData.length_AS" disabled>
                    <template slot="append">IN</template>
                  </el-input>
                </div>
                <div style="margin-left: 8px">
                  <el-input v-model="form.packData.width_AS" disabled>
                    <template slot="append">IN</template>
                  </el-input>
                </div>
                <div style="margin-left: 8px">
                  <el-input v-model="form.packData.height_AS" disabled>
                    <template slot="append">IN</template>
                  </el-input>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="体积(美制)">
              <el-input v-model.trim="form.packData.volume_AS" disabled />
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item label="体积重(美制)">
              <el-input v-model.trim="form.packData.volume_weight_AS" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="oversize参数">
              <el-input v-model.trim="form.packData.oversize" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="毛重(公制)">
              <el-input v-model="form.packData.weight_gross" disabled>
                <template slot="append">kg</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="毛重(美制)">
              <el-input v-model="form.packData.weight_gross_AS" disabled>
                <template slot="append">lbs</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="净重(公制)">
              <el-input v-model="form.packData.weight" disabled>
                <template slot="append">kg</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="净重(美制)">
              <el-input v-model="form.packData.weight_AS" disabled>
                <template slot="append">lbs</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="40HQ装箱量">
              <el-input v-model.trim="form.packData.hq_size" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="箱率">
              <el-input v-model.trim="form.packData.box_rate" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="运输包装尺寸">
              <div class="sizeStyle">
                <div>
                  <el-input v-model="form.packData.transport_length" disabled>
                    <template slot="append">CM</template>
                  </el-input>
                </div>
                <div style="margin-left: 8px">
                  <el-input v-model="form.packData.transport_width" disabled>
                    <template slot="append">CM</template>
                  </el-input>
                </div>
                <div style="margin-left: 8px">
                  <el-input v-model="form.packData.transport_height" disabled>
                    <template slot="append">CM</template>
                  </el-input>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="运输外箱体积">
              <el-input v-model.trim="form.packData.outside_transport_volume" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计量单位">
              <el-select v-model="form.packData.unit" disabled placeholder="">
                <el-option v-for="(item, index) of unitList" :key="index" :label="item.dict_data_name" :value="`${item.id}`" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="form.type == 1" :span="8">
            <el-form-item label="是否支持拆单">
              <el-input v-model.trim="splitBill" disabled />
            </el-form-item>
          </el-col>
          <el-col v-if="form.type == 1" :span="8">
            <el-form-item label="包装类型">
              <el-input v-model.trim="form.packData.packing_type" disabled />
            </el-form-item>
          </el-col>
          <el-col v-if="form.type == 1" :span="8">
            <el-form-item label="是否需要序列号">
              <el-input v-model.trim="form.packData.is_serial_number" disabled />
            </el-form-item>
          </el-col>
          <el-col v-if="form.type == 1" :span="8">
            <el-form-item label="是否带电">
              <el-input v-model.trim="form.packData.is_electric" disabled />
            </el-form-item>
          </el-col>
          <el-col v-if="form.type == 1" :span="8">
            <el-form-item label="打包设置">
              <el-input v-model.trim="form.packData.pack_set" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </div>

      <el-row>
        <mainTilte title="附件信息" />
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="商品说明书">
            <uploadFile :file-list="fileListDescription" :disabled="true" accept-type="application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" list-type="text" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="箱唛">
            <uploadFile :file-list="fileListmark" :disabled="true" accept-type="application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" list-type="text" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="其他资料">
            <uploadFile :file-list="fileListother" :disabled="true" accept-type="application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" list-type="text" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="整箱产品组成图片">
            <uploadFile :file-list="fileListpictures" :disabled="true" accept-type="image/jpeg,image/png" list-type="picture-card" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="产品包装细节图">
            <uploadFile :file-list="fileListdetail" :disabled="true" accept-type="image/jpeg,image/png" list-type="picture-card" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="dialog-footer-center">
        <span>
          <el-button type="primary" @click="$router.go(-1)">返回</el-button>
        </span>
      </div>
    </el-form>
  </div>
</template>

<script>
import { fileTransform } from '@/filters/index'
export default {
  data() {
    return {
      disabled: true,
      loading: false,
      form: {
        category_id: ''
      },
      roleCodeRule: {},
      fileList: [],
      freightList: [],
      fileListDescription: [], // 商品说明书上传
      fileListmark: [], // 箱唛上传
      fileListother: [], // 其他资料上传
      fileListpictures: [], // 整箱产品组成图片上传
      fileListdetail: [], // 产品包装细节图上传
      cateName: '',
      // 运营类型：1-代营 2-自营
      cooperationStatus: {
        1: '代营',
        2: '自营'
      },
      // 包装方式 ：1-普通商品 2-多箱包装
      splitOrder: {
        1: '普通商品',
        2: '多箱包装'
      },
      // 是否保价：1-保价 0-不保价
      isSurePriceList: {
        0: '不保价',
        1: '保价'
      },
      // 客户商品 包装类型 0-无包装 1-自带包装 2-特殊包装
      c_product: {
        0: '无包装',
        1: '自带包装',
        2: '特殊包装'
      },
      productConfig: {}
    }
  },
  computed: {
    // 是否支持拆单
    splitBill() {
      return this.form.packing_method === '普通商品' ? '否' : '是'
    }
  },
  mounted() {
    if (this.$route.query && this.$route.query.id) {
      this.getProductConfig()
      this.getDetailData(this.$route.query.id)
    }
    this.getUnitList()
  },

  methods: {
    // 获取 计算单位
    getUnitList() {
      const vm = this
      vm.$cache.getDic('unit').then(res => {
        vm.unitList = res
      })
    },
    getDetailData(id) {
      this.loading = true
      this.$http
        .get(`/product/${id}`)
        .then(res => {
          if (res.code === 10000) {
            this.form = res.data
            this.form.supplier_name = res.data.supplier_name || res.data.suppler_names
            console.log(this.form.otherData, 'form.otherData>>>>')
            this.cateName = this.form.category_name
              ? this.form.category_parent_name + ',' + this.form.category_name
              : ''
            this.form.operate_type = this.transform(
              this.form.operate_type,
              this.cooperationStatus
            )
            this.form.packing_method = this.transform(
              this.form.packing_method,
              this.splitOrder
            )
            this.form.insured_price = this.transform(
              this.form.insured_price,
              this.isSurePriceList
            )
            if (this.form.packData) {
              this.form.packData.packing_type = this.transform(
                this.form.packData.packing_type,
                this.c_product
              )
              this.form.packData.pack_set =
                this.form.packData.pack_set === 1 ? '独立打包' : '不设置'
              this.form.packData.is_serial_number =
                this.form.packData.is_serial_number === 1 ? '是' : '否'
              this.form.packData.is_electric =
                this.form.packData.is_electric === 1 ? '是' : '否'
            }
            if (this.form.otherData) {
              this.fileListDescription = fileTransform(
                this.form.otherData.description
              )
              this.fileListmark = fileTransform(this.form.otherData.box_mark)
              this.fileListother = fileTransform(this.form.otherData.other)
              this.fileListpictures = fileTransform(
                this.form.otherData.pictures
              )
              this.fileListdetail = fileTransform(
                this.form.otherData.detail_pictures
              )
            }
            // this.fileListDescription.push()
            this.loading = false
          }
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 获取体积
    getVolume(length, width, height) {
      const vm = this
      return +vm.$computed
        .accMul(length, vm.$computed.accMul(width, height))
        .toFixed(6)
    },
    // 获取体积重
    getVolumeWeight(length, width, height) {
      const volume = this.getVolume(length, width, height)
      return +(volume / this.productConfig.volume_factor).toFixed(6)
    },
    // 获取系统参数
    getProductConfig() {
      const vm = this
      vm.$http.get('/config/product').then(res => {
        vm.productConfig = res.data.product
      })
    },
    // 获取 oversize
    getOversize(length, width, height) {
      const vm = this
      return +vm.$computed
        .accAdd(
          length,
          vm.$computed.accMul(vm.$computed.accAdd(width, height), 2)
        )
        .toFixed(6)
    },
    transform(flied, obj) {
      return obj[flied] || ''
    }
  }
}
</script>

<style scoped>
.addExpense {
  padding: 15px;
  background: #fff;
}
/deep/.el-select,
.el-autocomplete,
.el-cascader {
  width: 100%;
}
.sizeStyle {
  display: flex;
}
.sizeStyle /deep/ .el-input__inner {
  border-right: 0;
}
.sizeStyle /deep/ .el-input-group__append {
  padding-left: 4px;
  padding-right: 4px;
  font-size: 14px;
  color: #333333;
  background: #fff;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader /deep/ .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>
